package com.qfedu.knowledge.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.knowledge.dao.UserDao;
import com.qfedu.knowledge.entity.bo.UserBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

/**
 * @ClassName SecurityUtils
 * @Author ykx
 * @Date 2024/4/26 10:33
 * @Version 1.0
 */
@Component
public class SecurityUtils {

    @Autowired
    private UserDao userDao;


    public UserBO getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();

        QueryWrapper<UserBO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());

        return userDao.selectOne(queryWrapper);

    }

}
